﻿namespace Linq常用扩展方法;

class Program
{
    static void Main(string[] args)
    {
        List<Employee> list = new List<Employee>();
        list.Add(new Employee { Id = 1, Name = "jerry", Age = 28, Gender = true, Salary = 5000 });
        list.Add(new Employee { Id = 2, Name = "jim", Age = 33, Gender = true, Salary = 3000 });
        list.Add(new Employee { Id = 3, Name = "lily", Age = 35, Gender = false, Salary = 9000 });
        list.Add(new Employee { Id = 4, Name = "lucy", Age = 16, Gender = false, Salary = 2000 });
        list.Add(new Employee { Id = 5, Name = "kimi", Age = 25, Gender = true, Salary = 1000 });
        list.Add(new Employee { Id = 6, Name = "nancy", Age = 35, Gender = false, Salary = 8000 });
        list.Add(new Employee { Id = 7, Name = "zack", Age = 35, Gender = true, Salary = 8500 });
        list.Add(new Employee { Id = 8, Name = "jack", Age = 33, Gender = true, Salary = 8000 });

        //int[] nums = [1, 2, 3, 4, 5, 6, 7, 8, 9, 10];
        /* Random rand = new Random();
         var newNums = nums.OrderBy(e =>
         {
             var value = Guid.NewGuid();
             Console.WriteLine($"current random value:{value},current element:{e}");
             return value;
         });
         foreach (var num in newNums)
         {
             Console.WriteLine(num);
         }*/
        // var items = list.Select(p => new { Name = p.Name, Age = p.Age, Salary = p.Salary });
        /*var items = list.Select(p=>new {p.Id,p.Age,p.Salary}).OrderBy(p=>p.Salary).ThenBy(p=>p.Age);
        foreach (var item in items)
        {
            Console.WriteLine($"Name:{item.Id}|Age:{item.Age}|Salary:{item.Salary}");
        }*/
        
        /*
        var result = list
            .GroupBy(p => p.Age)
            .Select(g =>
                new {
                    Age = g.Key,
                    MaxSalary = g.Max(p=>p.Salary),
                    MinSalary = g.Min(p=>p.Salary),
                    Count = g.Count(),
                    HasWoman = g.FirstOrDefault(p => !p.Gender)!=null?"是":"否"
                })
            .OrderBy(p=>p.Age);
        foreach (var item in result)
        {
            Console.WriteLine($"分组年龄：{item.Age}|最高工资为：{item.MaxSalary}|最低工资为：{item.MinSalary}|每组人数为：{item.Count}|是否有女士：{item.HasWoman}");
        }*/
        /*
        var result = list
            .Where(e => e.Id > 2)
            .GroupBy(e => e.Age)
            .OrderBy(g => g.Key)
            .Take(3)
            .Select(
                g => new
                {
                    Age = g.Key,
                    Count = g.Count(),
                    AverageSalary = g.Average(e => e.Salary)
                });
        foreach (var item in result)
        {
            Console.WriteLine($"age:{item.Age}|count:{item.Count}|averageSalary:{item.AverageSalary}");
        }*/
        string str = "61,90,100,99,18,22,38,66,80,93,55,50,89";
        string[] strs = str.Trim().Split(',');
        double result = 0;
        for (int i = 0; i < strs.Length; i++)
        {
            result += int.Parse(strs[i]);;
        }
        double average = result / strs.Length;
        Console.WriteLine(average);
        var result2 = str.Trim().Split(',').Select(s => int.Parse(s)).Average();
        Console.WriteLine(result2);

        string word = "hello world,hahah,heiheihei";
        var result3 = word
            .ToLower()
            .Where(char.IsLetter)
            .GroupBy(c => c)
            .OrderByDescending(g => g.Count())
            .Where(g => g.Count() > 2)
            .Select(g => new { CharName = g.Key,Count = g.Count() });
        foreach (var item in result3)
        {
            Console.WriteLine($"char name:{item.CharName}|char count:{item.Count}");
        }
    }
}